﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>导航菜单详情</title>
    <link href="../lib/layui/css/layui.css" rel="stylesheet" />
    <link href="../x-admin/css/font.css" rel="stylesheet" />
    <link href="../x-admin/css/xadmin.css" rel="stylesheet" />
</head>
<body>
    <div class="layui-card-body" style="">
        <form class="layui-form" id="navform" lay-filter="navform">
            <div class="layui-form-item">
                <label class="layui-form-label">层级</label>
                <div class="layui-input-block">
                    <select name="level" lay-filter="level" lay-verify="required">
                        <option value="">请选择</option>
                        <option value="1">一级</option>
                        <option value="2">二级</option>
                        <option value="3">三级</option>
                        <option value="4">四级</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">父级</label>
                <div class="layui-input-block">
                    <select name="parentId" id="parentId" lay-filter="parentId" lay-verify="required"></select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">Id</label>
                <div class="layui-input-block">
                    <input type="text" name="id" readonly autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">显示序号</label>
                <div class="layui-input-block">
                    <input type="text" name="index" readonly autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">编号</label>
                <div class="layui-input-block">
                    <input type="text" name="code" required lay-verify="required" placeholder="请输入CODE" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name" required lay-verify="required" placeholder="请输入名称" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">文件夹</label>
                <div class="layui-input-block">
                    <input type="text" name="directory" placeholder="请输入文件夹" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">是否启用</label>
                    <div class="layui-input-inline">
                        <input type="checkbox" name="navicatflag" lay-skin="switch">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">是否统计</label>
                    <div class="layui-input-inline">
                        <input type="checkbox" name="isstatistics" lay-skin="switch">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">是否目录</label>
                    <div class="layui-input-inline">
                        <input type="checkbox" name="isdirectory" lay-skin="switch">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">图标</label>
                <div class="layui-input-block">
                    <input type="text" name="img" required lay-verify="required" placeholder="请输入图标" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">链接地址</label>
                <div class="layui-input-block">
                    <input type="text" name="href" placeholder="请输入地址" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <textarea name="note" placeholder="请输入内容" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" type="button" lay-submit lay-filter="submit">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
    <script src="../lib/layui/layui.js"></script>
    <script type="text/javascript" src="../lib/Base64/base64.min.js"></script>
    <script>
        function getrequest() {
            var url = location.search; //获取url中"?"符后的字串
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                }
            }
            return theRequest;
        }
        layui.use('form', function () {
            var form = layui.form;
            var $ = layui.$;
            form.render(null, navform);
            form.on('select(level)', function (obj) {
                var level = obj.value;//被选中的值
                if (level != null && level != "") {
                    loadParent(level);
                }
                return false;
            });

            form.on('select(parentId)', function (obj) {
                var id = obj.value;//被选中的值
                if (id != null && id != "") {
                    getId(id);
                }
                return false;
            });

            //监听提交
            form.on('submit(submit)', function (data) {
                if (data.field.navicatflag == "on") {
                    data.field.navicatflag = 1;
                }else {
                    data.field.navicatflag = 0;
                }
                if (data.field.isstatistics == "on") {
                    data.field.isstatistics = 1;
                } else {
                    data.field.isstatistics = 0;
                }


                if (data.field.isdirectory == "on") {
                    data.field.isdirectory = 1;
                } else {
                    data.field.isdirectory = 0;
                }

                data.field.level = parseInt(data.field.level);
                data.field.index = parseInt(data.field.index);
                data.field.id = parseInt(data.field.id);
                data.field.parentId = parseInt(data.field.parentId);
                layui.$.ajax({
                    type: "post",
                    url: localStorage.getItem('hosttingaddr') + '/api/main/updatemenusnav',
                    dataType: 'json',
                    contentType: 'application/json;charset=utf-8',
                    data: JSON.stringify(data.field),
                    success: function (re) {
                        layer.msg('保存成功!', { icon: 1, time: 1000 });
                        window.parent.location.reload();
                        var index = parent.layer.getFrameIndex(window.name); //获取当前窗口的name
                        parent.layer.close(index);
                    },
                    error: function (re) {
                        layer.msg('保存失败!', { icon: 1, time: 1000 });
                        window.parent.location.reload();
                        var index = parent.layer.getFrameIndex(window.name); //获取当前窗口的name
                        parent.layer.close(index);
                    }
                });
                return false;
            });

            //加载父级导航下拉数据
            function loadParent(level) {
                $("#parentId").empty();
                $.ajax({
                    url: localStorage.getItem('hosttingaddr') + '/api/main/getparentsbylevel?level=' + level,
                    type: 'get',
                    async: false,
                    success: function (re) {
                        if (re.code == 0) {
                            if (re.data.length > 0) {
                                for (var i = 0; i < re.data.length; i++) {
                                    $("#parentId").append("<option value='" + re.data[i].id + "' data-index='" + re.data[i].index + "' >" + re.data[i].name + "</option>");
                                }
                                form.render('select');
                                getId(re.data[0].id);
                            }
                            
                        }
                    }
                });
            };

            function getId(parentId) {
                if (parentId != null && parentId != "") {
                    $.ajax({
                        url: localStorage.getItem('hosttingaddr') + '/api/main/getnavid?parentId=' + parentId,
                        type: 'get',
                        async: false,
                        success: function (re) {
                            if (re.code == 0) {
                                form.val("navform", {
                                    "id": re.data.id,
                                    "index": re.data.index
                                });
                            }
                        }
                    });
                }
            }

            
        });
    </script>
</body>
</html>